package br.ufla.pgcc.oomeasures.plugin.chart;

import java.awt.Color;
import java.awt.Font;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import br.ufla.pgcc.oomeasures.ItemMeasured;

public class Chart {


	/**
	 * Cria um grafico de linhas com os itens medidos
	 *
	 */
	public JFreeChart createLineChart(ItemMeasured itemsMeasured) {

		
		DefaultCategoryDataset lineChartDataset = new DefaultCategoryDataset();
		String projectName = "";
		if (itemsMeasured != null) {
			List<ItemMeasured> items = itemsMeasured.getChildren();
			projectName = itemsMeasured.getName();

			for (ItemMeasured item : items) {
				lineChartDataset.addValue(item.getValue(), projectName, item.getName());
			}
		}
		
		JFreeChart chart = ChartFactory.createLineChart("Measures of the project "+projectName, 
				                                        "Measures",
				                                        "Values",
				                                        lineChartDataset,
				                                        PlotOrientation.HORIZONTAL,
				                                        false, // legend
				                                        true,  // tooltips
				                                        false);// urls
		
		chart.getTitle().setFont(new Font("MS Sans Serif", Font.BOLD, 14));
		CategoryPlot categoryPlot = (CategoryPlot)chart.getPlot();
       
		//final CategoryAxis xAxis = (CategoryAxis)categoryPlot.getDomainAxis();
        //xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
        
        categoryPlot.setBackgroundPaint(Color.white);
        categoryPlot.setDomainGridlinePaint(Color.black);
        categoryPlot.setRangeGridlinePaint(Color.black);
        
        final LineAndShapeRenderer renderer = new LineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, true);
        renderer.setSeriesShapesVisible(0, true);
        categoryPlot.setRenderer(renderer);
        
        final NumberAxis rangeAxis = (NumberAxis) categoryPlot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		chart.fireChartChanged();

		return chart;  
		
	}
}
